package com.lightsh.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class SampleListItemView extends AbstractListItemView implements View.OnClickListener{

	private TextView mTextView;
	public SampleListItemView(Context context) {
		super(context);
	}
	public SampleListItemView(Context context, OnItemSelectListener listener){
		super(context, listener);
	}

	@Override
	public void initView() {
		// 初始化view
		View view = LayoutInflater.from(this.mContext).inflate(android.R.layout.simple_list_item_1, null);
		mTextView = (TextView) view.findViewById(android.R.id.text1);
		mTextView.setText("init value");
		// 如果允许选中，需要设置子view的onclicklistener
		if(mItemSelectListener != null){
			mTextView.setOnClickListener(this);
		}
		addView(view);
	}

	@Override
	public void updateView(AbstractListItem item, int position) {
		// 可以对item做限制
		if(!(item instanceof SampleListItem)){
			throw new IllegalArgumentException("item must instance of SampleListItem");
		}
		SampleListItem sampleItem = (SampleListItem) item;
		
		// 初始化index和selected
		mIndex = position;
		mbSelected = false;
		
		// 更新数据
		mTextView.setText(sampleItem.getTitle());
	}

	@Override
	public void onClick(View v) {
		// 这里做点击的判断和处理
		if(v.getId() == android.R.id.text1){
			// 可以增加是否允许点击的判断
			mbSelected = !mbSelected;
			// 更新ui
			if(mbSelected){
				mTextView.setText("selected");
			}else{
				mTextView.setText("unselected");
			}
			notifySelectedChange();
		}
		
	}

}
